package amq.demo.thds;

import javax.jms.Connection;
import javax.jms.Destination;
import javax.jms.ExceptionListener;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageConsumer;
import javax.jms.MessageListener;
import javax.jms.Session;

import org.apache.activemq.ActiveMQConnectionFactory;

import amq.demo.SentProducer;
import amq.demo.SentProducer.Task;

/**
 * BD: Answer Queue
 * @author John
 *
 */
public class AQThd implements Runnable, MessageListener, ExceptionListener{
	public boolean isStop = false;
	String queueName = null;
	String brokerURI = null;	
	
	public AQThd(String qn, String bui)
	{
		this.queueName = qn;
		this.brokerURI = bui;
	}

	@Override
	public void run() {
		try {
			System.out.printf("\t[Info] Opening Answer Queue(%s)...\n", queueName);
			// Create a ConnectionFactory
            ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory(brokerURI);

            // Create a Connection
            Connection connection = connectionFactory.createConnection();
            connection.start();

            connection.setExceptionListener(this);

            // Create a Session
            Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);

            // Create the destination (Topic or Queue)
            Destination destination = session.createQueue(queueName);

            // Create a MessageConsumer from the Session to the Topic or Queue
            MessageConsumer consumer = session.createConsumer(destination);

            consumer.setMessageListener(this);
            while(!isStop)
            {
            	Thread.sleep(5000);
            }
            System.out.printf("\t[Info] Closing Answer Queue...\n");
            consumer.close();
            session.close();
            connection.close();
        }
        catch (Exception e) {
            System.out.println("Caught: " + e);
            e.printStackTrace();
        }
		
	}

	@Override
	public void onMessage(Message answer) {
		System.out.printf("\t[Info] Receive Answer Message:\n%s\n", answer);
		try
		{
			String resp = null;
			if(answer.getBooleanProperty("Correct"))
			{
				resp = String.format("%s|Pass|-|-|-", answer.getStringProperty("ServiceName"));
			}
			else
			{
				resp = String.format("%s|Fail|%s|%s|%s", answer.getStringProperty("ServiceName"),
						                                 answer.getStringProperty("Category"),
						                                 answer.getStringProperty("Reason"),
						                                 answer.getStringProperty("Suggestion"));
			}
			SentProducer.Callback(answer.getStringProperty("TID"), resp);
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}
	}

	@Override
	public void onException(JMSException expt) {
		System.err.printf("\t[Expt] Exception Message:\n%s\n", expt);		
	}
	
	public void addTask(Task task)
	{
		
	}
}
